var fs = require('fs');
function strMapToObj(strMap) {
  let obj = Object.create(null);
  for (let [k,
    v]of strMap) {
    obj[k] = v;
  }
  return obj;
}

function strMapToJson(strMap) {
  return JSON.stringify(strMapToObj(strMap));
}
var DataList = [];
var Datas;
fs.readdir("/Users/eh/Documents/mywork/abc", function(err, files) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(files.length);
  files.forEach(function(filename) {
    var myMap = new Map();
    var readline = require('linebyline'),
      rl = readline('/Users/eh/Documents/mywork/abc/'+filename);
    var temp1;
    rl.on('line', function(line, lineCount, byteCount) {
      var regex = /^【(.*)】/; //有【】
      var regex1 = /^[^【]|^$/;
      var regex2 = /^[^【】$]/; //没有中括号
      var regex3 = /^$/; //空白行
      if (line.match(regex)) { //匹配【】
        // console.log(line);
        temp1 = line.match(regex).input.toString();
        myMap.set(temp1, '');
      } else if (line.match(regex1)) {
        if (line.match(regex2)) { //有值
          var value1 = myMap.get(temp1);
          var value = line.match(regex2).input;
          myMap.set(temp1, value1 + value);
        }
        if (line.match(regex3)) { //空白行
          var valueBlank1 = myMap.get(temp1);
          var valueBlank = line.match(regex3).input;
          myMap.set(temp1, valueBlank1 + valueBlank);
        }
      }
    }).on('close', function() {
       Datas = strMapToJson(myMap);
       DataList.push(Datas);
       fs.writeFile("./data.json", DataList, (err) => {})

    });

  });

});
